import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';
const baseUrl = '/sales/api/v1';

const getInitdesc = formatMessage({
    id: 'api.message.getInitdesc',
    defaultMessage: '初始化'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询详情'
});
const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '修改'
});
const exportDataDesc = formatMessage({
    id: 'api.message.exportDataDesc',
    defaultMessage: '导出数据'
});
const importTempDesc = formatMessage({
    id: 'api.message.importTempDesc',
    defaultMessage: '导入模板'
});
const getDealerResourceListDesc = formatMessage({
    id: 'api.message.getDealerResourceListDesc',
    defaultMessage: '产品查询'
});
//初始化
export const getInitData = () => request(`${baseUrl}/ui/dealerResourceList/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitdesc));

// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealerResourceLists/forCurrentUser?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/dealerResourceLists/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
//修改
export const updateApplyReport = (id, data) => request(`${baseUrl}/dealerResourceLists/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));
// 导出
export const exportData = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/dealerResourceLists/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDataDesc));
};
//导入清单模板
export const importTemp = () => request(`${baseUrl}/dealerResourceLists/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, importTempDesc)).then(res => res.data);
//导入存销比
export const storeSaleRatioImportTemp = () => request(`${baseUrl}/dealerResourceLists/storeSaleRatioImportTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, importTempDesc)).then(res => res.data);
// 产品查询
export const getDealerResourceList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealerResourceLists/productResources?${queryString}`, null, dto.DealerResourceList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerResourceListDesc));
};

